{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "coordinated-scheduling",
   "metadata": {},
   "source": [
    "# Get Europe electricity carbon intensity\n",
    "\n",
    "From https://ourworldindata.org/grapher/carbon-intensity-electricity download the data directly in CSV."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "aquatic-distance",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Entity</th>\n",
       "      <th>Code</th>\n",
       "      <th>Year</th>\n",
       "      <th>carbon_intensity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>629</th>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>GBR</td>\n",
       "      <td>2020</td>\n",
       "      <td>209</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Entity Code  Year  carbon_intensity\n",
       "629  United Kingdom  GBR  2020               209"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import json\n",
    "df = pd.read_csv(\"./eu-carbon-intensity-electricity.csv\")\n",
    "df.columns=['Entity', 'Code', 'Year', 'carbon_intensity']\n",
    "df.tail(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "interesting-proposition",
   "metadata": {},
   "source": [
    "We have to set these values from `df` into the JSON.\n",
    "\n",
    "It's easy as `Code` is the ISO-3 Country code we use as key in the JSON."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "instructional-taylor",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'year': 2020,\n",
       " 'fossil_TWh': 113.326,\n",
       " 'geothermal_TWh': 0.0,\n",
       " 'hydroelectricity_TWh': 5.91884,\n",
       " 'nuclear_TWh': 45.67,\n",
       " 'solar_TWh': 12.9696587058,\n",
       " 'wind_TWh': 75.645820869,\n",
       " 'country_id': 228,\n",
       " 'country_name': 'UK',\n",
       " 'iso_code': 'GBR',\n",
       " 'official_name_en': 'United Kingdom of Great Britain and Northern Ireland',\n",
       " 'region': 'Europe',\n",
       " 'total_TWh': 253.53031957480002}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('./global_energy_mix.json', \"r\") as f:\n",
    "  world = json.load(f)\n",
    "world.get(\"GBR\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "hungarian-metabolism",
   "metadata": {},
   "outputs": [],
   "source": [
    "def set_carbon_intensity(row):\n",
    "    global world\n",
    "    if row['Code'] in world.keys():\n",
    "        world[row['Code']][\"carbon_intensity\"]=row['carbon_intensity']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "thick-window",
   "metadata": {},
   "outputs": [],
   "source": [
    "_=df.query(\"Year==2020\").apply(set_carbon_intensity, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "lightweight-courage",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert world.get(\"GBR\").get(\"carbon_intensity\") == 209\n",
    "assert world.get(\"FRA\").get(\"carbon_intensity\") == 55"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "metallic-manufacturer",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"global_energy_mix.json\", \"w\") as outfile:\n",
    "    json.dump(world, outfile, indent=4, sort_keys=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sealed-dairy",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "leximpact",
   "language": "python",
   "name": "leximpact"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
